﻿using HR.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;

namespace HR.Controllers
{
    public class RadisTestController : Controller
    {
        private readonly IDistributedCache idc;

        public RadisTestController(IDistributedCache idc)
        {
            this.idc = idc;
        }
        //[TypeFilter(typeof(RoleFilterAttribute))]
        public object Index()
        {
            string data = GetDatetimeNow();
            Console.WriteLine(data);
            return View();
        }
        private string GetDatetimeNow()
        {
            string key = "RadisTestControllerDateTime";
            string date = idc.GetString(key);
            if (string.IsNullOrEmpty(date))
            {
                date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
                //写入缓存，并设置第三个参数是到期时间30s
                idc.SetString(key, date, new DistributedCacheEntryOptions()
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(300)
                });
            };
            return date;
        }
    }
}
